package net.mikoo.seals.manage.web.common.directive;

import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import net.mikoo.seals.common.enums.ApplyStatusEnum;
import net.mikoo.seals.common.enums.ApplyTypeEnum;
import net.mikoo.seals.common.enums.DevicesStatusEnum;
import net.mikoo.seals.common.enums.DevicesUsedTypeEnum;
import net.mikoo.seals.common.enums.PartyStatusEnum;
import net.mikoo.seals.common.enums.NoticeStatusEnum;
import net.mikoo.seals.common.enums.ScheduleTypeEnum;
import net.mikoo.seals.common.enums.TakeStatusEnum;
import net.mikoo.seals.common.enums.UsedStatusEnum;
import net.mikoo.seals.common.enums.VerifyStatusEnum;
import net.mikoo.seals.common.enums.WorkflowStatusEnum;
import net.mikoo.seals.common.enums.WorkflowTypeEnum;

public class EnumsDirective implements TemplateDirectiveModel {

	private Logger logger = Logger.getLogger(EnumsDirective.class);

	@SuppressWarnings("rawtypes")
	@Override
	public void execute(Environment env, Map params, TemplateModel[] model, TemplateDirectiveBody body) throws TemplateException, IOException {
		try {

			String object = params.get("object").toString();

			String code = params.get("code").toString();

			String msg = "";

			switch (object.toString()) {
			case "APPLY_STATUS":
				msg = ApplyStatusEnum.getByCode(code).getMessage();
				break;
				
			case "APPLY_TYPE":
				
				msg = ApplyTypeEnum.getByCode(code).getMessage();
				break;
				
			case "DEVICES_STATUS":

				msg = DevicesStatusEnum.getByCode(code).getMessage();

				break;
			case "DEVICES_USED_TYPE":

				msg = DevicesUsedTypeEnum.getByCode(code).getMessage();

				break;
			case "PARTY_STATUS":
				msg = PartyStatusEnum.getByCode(code).getMessage();

				break;
			case "SCHEDULE_STATUS":

				msg = NoticeStatusEnum.getByCode(code).getMessage();

				break;
			case "SCHEDULE_TYPE":

				msg = ScheduleTypeEnum.getByCode(code).getMessage();

				break;
			case "TAKE_STATUS":

				msg = TakeStatusEnum.getByCode(code).getMessage();
				break;
			case "USE_STATUS":
				msg = UsedStatusEnum.getByCode(code).getMessage();
				break;
			case "VERIFY_STATUS":
				msg = VerifyStatusEnum.getByCode(code).getMessage();

				break;
			case "WORKFLOW_STATUS":

				msg = WorkflowStatusEnum.getByCode(code).getMessage();
				break;
			case "WORKFLOW_TYPE":

				msg = WorkflowTypeEnum.getByCode(code).getMessage();
				break;

			default:
				break;
			}

			@SuppressWarnings("unchecked")
			Map<String, TemplateModel> paramWrap = new HashMap<String, TemplateModel>(params);
			paramWrap.put("msg", DEFAULT_WRAPPER.wrap(msg));

			Map<String, TemplateModel> origMap = DirectiveUtil.addParamsToVariable(env, paramWrap);

			body.render(env.getOut());

			DirectiveUtil.removeParamsFromVariable(env, paramWrap, origMap);

		} catch (Exception e) {
			logger.error("", e);
		}
	}

}